package com.pi.nbcenter.device.controller.iot;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.pi.base.dto.result.AppResult;
import com.pi.base.dto.result.ServiceResult;
import com.pi.iot.repository.constants.PiInsEnum;
import com.pi.nbcenter.device.bean.dto.ins.InsLockPasswordParam;
import com.pi.nbcenter.device.service.pi.external.PiInsService;

@RequestMapping("/lockpassword")
@RestController
public class LockPasswordController {
  @Autowired
  private PiInsService piInsService;
  
  @PostMapping("rise")
  public AppResult localPasswordEventRise(String imei, Long userId, Long sourceId)
      throws Exception{
    InsLockPasswordParam insParam = new InsLockPasswordParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setUserId(userId);
    insParam.setInsEnum(PiInsEnum.PWD_RISE);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
  @PostMapping("/add")
  public AppResult addPasswordRemote(String imei, Long userId, String password, Long sourceId)
      throws Exception{
    InsLockPasswordParam insParam = new InsLockPasswordParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setUserId(userId);
    insParam.setPassword(password);
    insParam.setInsEnum(PiInsEnum.PWD_ADD);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
  @PostMapping("/delete")
  public AppResult deleteUserPassword(String imei, Long userId, Short passwordId, Long sourceId)
      throws Exception{
    InsLockPasswordParam insParam = new InsLockPasswordParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setUserId(userId);
    insParam.setPasswordId(passwordId);
    insParam.setInsEnum(PiInsEnum.PWD_DEL);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
  @PostMapping("/clear")
  public AppResult clearUserPassword(String imei, Long userId, Long sourceId) 
      throws Exception{
    InsLockPasswordParam insParam = new InsLockPasswordParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setUserId(userId);
    insParam.setInsEnum(PiInsEnum.PWD_CLEAR);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
  @PostMapping("/temp")
  public AppResult tempPasswordEventRise(String imei, String password, Long sourceId) 
      throws Exception{
    InsLockPasswordParam insParam = new InsLockPasswordParam();
    insParam.setImei(imei);
    insParam.setSourceId(sourceId);
    insParam.setPassword(password);
    insParam.setInsEnum(PiInsEnum.TEMP_PWD);
    ServiceResult<String> result = 
        piInsService.sendIns(insParam);
    if(result.equalSuccess())
      return AppResult.OK;
    return AppResult.newFailResult(result.getMsg(), result.getErrorCode());
  }
}
